package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.config.R;
import com.itheima.config.ThreadLocals;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 查询购物车
     * 此接口完成 登录后才能查询到左边分类
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> ShoppingCart() {

        //  操作用户ID
        Object UserId = ThreadLocals.getthread().get();

        // 用户ID 对应 一个购物车表的 Userid
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, UserId);

        // 获取用户下面全部的购物车
        List<ShoppingCart> list = shoppingCartService.list(wrapper);

        return R.success(list);
    }

    /**
     * 点餐添加到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

        // 拿到用户的ID
        Long UserId = (Long) ThreadLocals.getthread().get();

        // 获取 这个用户下 是否有购物车
        LambdaQueryWrapper<ShoppingCart> dishIdWrapper = new LambdaQueryWrapper<>();
        dishIdWrapper.eq(ShoppingCart::getUserId,UserId);

        // 添加的是菜品 /  还是 套餐  给的套件
        if (shoppingCart.getDishId() != null){
            // 添加的是菜品
            dishIdWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

        }else {
            // 添加的是套餐
            dishIdWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        // 用户 根据菜品  或者 套餐的条件 查询是否有数据   2
        ShoppingCart one = shoppingCartService.getOne(dishIdWrapper);

        // 用户是否第一次添加菜品 / 套餐 到购物车
        if (one == null){
            // 第一次添加   菜品  或 套餐都 执行
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(UserId);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }else {
            // 不是第一次添加菜品 / 套餐 到购物车
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
            return R.success(one);
        }



    }

    /**
     * 清空购物车   TODO 未测试
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){

        Long UserId = (Long)ThreadLocals.getthread().get();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, UserId);

        shoppingCartService.remove(queryWrapper);

        return R.success("清空购物车成功");
    }

    /**
     * 取消购物车上的菜品
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){

        // 获取用户ID
        Long UserId = (Long)ThreadLocals.getthread().get();

        // 确定这个购物车是这个用户的
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,UserId);

        // 判断删除的是 菜品  还是 套餐
        if (shoppingCart.getDishId() != null){
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        // 根据上面的条件查询这条数据
        ShoppingCart one = shoppingCartService.getOne(wrapper);

        // 判断这个菜品 货套餐的数量
        if (one.getNumber() == 1){

            /**
             * 这里只能选择 == 1
             * 因为当一个菜品或套餐为0 就应该从购物车把这个数据清楚掉
             */
            one.setNumber(one.getNumber()-1);
            shoppingCartService.removeById(one.getId());
        }else {
            /**
             * 当不为1的 时候 正常去除购物车上的数量
             * 进行修改
             */
            one.setNumber(one.getNumber()-1);
            shoppingCartService.updateById(one);
        }

        return R.success(one);
    }


}
